<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css">
  <script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zhangyh.fun","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="NLTK 模块详解1. 介绍 nltk 是一个 python 工具包，用来处理与自然语言相关的东西，包括分词（tokenize）、词性标注（POS）、文本分类等，是较为好用的现成工具。目前支持英文分词，而不支持中文分词。">
<meta property="og:type" content="article">
<meta property="og:title" content="NLTK 模块详解">
<meta property="og:url" content="https://zhangyh.fun/2021/08/13/NLTK%20%E6%A8%A1%E5%9D%97%E8%AF%A6%E8%A7%A3/index.html">
<meta property="og:site_name" content="XiaoPang の Blog">
<meta property="og:description" content="NLTK 模块详解1. 介绍 nltk 是一个 python 工具包，用来处理与自然语言相关的东西，包括分词（tokenize）、词性标注（POS）、文本分类等，是较为好用的现成工具。目前支持英文分词，而不支持中文分词。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-13T01:40:00.000Z">
<meta property="article:modified_time" content="2021-08-17T02:06:57.193Z">
<meta property="article:author" content="XiaoPang">
<meta property="article:tag" content="python">
<meta property="article:tag" content="NLTK">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://zhangyh.fun/2021/08/13/NLTK%20%E6%A8%A1%E5%9D%97%E8%AF%A6%E8%A7%A3/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>NLTK 模块详解 | XiaoPang の Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?54b35e78bac318b678b24fbb33049203";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
  </script>


</head>

<body itemscope itemtype="http://schema.org/WebPage">

  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">XiaoPang の Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
    <script src="https://v1.hitokoto.cn/?c=f&encode=js&select=%23hitokoto" defer></script>

      <p class="site-subtitle" itemprop="description" id="hitokoto"><span id="hitokoto_text">记录生活中的点点滴滴</span></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">19</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">11</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">34</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zhangyh.fun/2021/08/13/NLTK%20%E6%A8%A1%E5%9D%97%E8%AF%A6%E8%A7%A3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="XiaoPang">
      <meta itemprop="description" content="少说，多做">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XiaoPang の Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          NLTK 模块详解
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于：</span>

              <time title="创建时间：2021-08-13 09:40:00" itemprop="dateCreated datePublished" datetime="2021-08-13T09:40:00+08:00">2021-08-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于：</span>
                <time title="修改时间：2021-08-17 10:06:57" itemprop="dateModified" datetime="2021-08-17T10:06:57+08:00">2021-08-17</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/python/" itemprop="url" rel="index"><span itemprop="name">python</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <br>
            <span class="post-meta-item" title="本文字数">
            <span class="post-meta-item-icon">
            <i class="fa fa-file-word"></i>
            </span>
           <span class="post-meta-item-text">本文字数：</span>
           <span title="post.wordcount">
                1.5k 字
            </span>
            </span>
            <span class="post-meta-item" title="阅读时长">
            <span class="post-meta-item-icon">
            <i class="fa fa-clock"></i>
            </span>
          <span class="post-meta-item-text">阅读时长：</span>
           <span title="post.min2read">
                8 分钟
            </span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="NLTK-模块详解"><a href="#NLTK-模块详解" class="headerlink" title="NLTK 模块详解"></a>NLTK 模块详解</h2><h3 id="1-介绍"><a href="#1-介绍" class="headerlink" title="1. 介绍"></a>1. 介绍</h3><ul>
<li><code>nltk</code> 是一个 <code>python</code> 工具包，用来处理与自然语言相关的东西，包括分词（<code>tokenize</code>）、词性标注（<code>POS</code>）、文本分类等，是较为好用的现成工具。目前支持英文分词，而不支持中文分词。<span id="more"></span>
<h3 id="2-安装模块"><a href="#2-安装模块" class="headerlink" title="2. 安装模块"></a>2. 安装模块</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install nltk</span><br></pre></td></tr></table></figure></li>
<li>注意：<code>nltk</code> 相当于一个总模块，如果需要正常使用，还需要自定义安装内容。<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> nltk</span><br><span class="line">nltk.download()</span><br><span class="line"><span class="comment"># showing info https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml</span></span><br><span class="line"><span class="comment"># 代码运行后，会弹出NLTK Downloader窗口，可以安装使用的模块。</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="3-基础方法"><a href="#3-基础方法" class="headerlink" title="3. 基础方法"></a>3. 基础方法</h3><h4 id="3-1-文本按照句子进行分割-sent-tokenize"><a href="#3-1-文本按照句子进行分割-sent-tokenize" class="headerlink" title="3.1 文本按照句子进行分割 sent_tokenize()"></a>3.1 文本按照句子进行分割 <code>sent_tokenize()</code></h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-  </span></span><br><span class="line"><span class="keyword">from</span> nltk.tokenize <span class="keyword">import</span> sent_tokenize  </span><br><span class="line">  </span><br><span class="line">input_str = <span class="string">&quot;A wrapper around a sequence of simple (string) tokens. which is intended to support initial exploration of texts (via the interactive console).&quot;</span>  </span><br><span class="line"></span><br><span class="line">tokens = sent_tokenize(input_str)  </span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(tokens), tokens)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">&lt;class &#x27;list&#x27;&gt; [&#x27;A wrapper around a sequence of simple (string) tokens.&#x27;, &#x27;which is intended to support initial exploration of texts (via the interactive console).&#x27;]</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="3-2-对句子进行分词-word-tokenize"><a href="#3-2-对句子进行分词-word-tokenize" class="headerlink" title="3.2 对句子进行分词 word_tokenize()"></a>3.2 对句子进行分词 <code>word_tokenize()</code></h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-  </span></span><br><span class="line"><span class="keyword">from</span> nltk.tokenize <span class="keyword">import</span> word_tokenize</span><br><span class="line">  </span><br><span class="line">input_str = <span class="string">&quot;A wrapper around a sequence of simple (string) tokens. which is intended to support initial exploration of texts (via the interactive console).&quot;</span>  </span><br><span class="line"></span><br><span class="line">tokens = word_tokenize(input_str)  </span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(tokens), tokens)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">&lt;class &#x27;list&#x27;&gt; [&#x27;A&#x27;, &#x27;wrapper&#x27;, &#x27;around&#x27;, &#x27;a&#x27;, &#x27;sequence&#x27;, &#x27;of&#x27;, &#x27;simple&#x27;, &#x27;(&#x27;, &#x27;string&#x27;, &#x27;)&#x27;, &#x27;tokens&#x27;, &#x27;.&#x27;, &#x27;which&#x27;, &#x27;is&#x27;, &#x27;intended&#x27;, &#x27;to&#x27;, &#x27;support&#x27;, &#x27;initial&#x27;, &#x27;exploration&#x27;, &#x27;of&#x27;, &#x27;texts&#x27;, &#x27;(&#x27;, &#x27;via&#x27;, &#x27;the&#x27;, &#x27;interactive&#x27;, &#x27;console&#x27;, &#x27;)&#x27;, &#x27;.&#x27;]</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>注意：对文本按照句子，分词是句子级。因此需要先分句，在逐句分词，否则效果较差。</li>
</ul>
<h4 id="3-3-分词词性标注-pos-tag"><a href="#3-3-分词词性标注-pos-tag" class="headerlink" title="3.3 分词词性标注 pos_tag()"></a>3.3 分词词性标注 <code>pos_tag()</code></h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> nltk <span class="keyword">import</span> pos_tag</span><br><span class="line"><span class="keyword">from</span> nltk <span class="keyword">import</span> pos_tag  </span><br><span class="line"><span class="keyword">from</span> nltk.tokenize <span class="keyword">import</span> word_tokenize, sent_tokenize  </span><br><span class="line"><span class="keyword">from</span> nltk.text <span class="keyword">import</span> Text  </span><br><span class="line">  </span><br><span class="line">input_str = <span class="string">&quot;A wrapper around a sequence of simple (string) tokens. which is intended to support initial exploration of texts (via the interactive console).&quot;</span>  </span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">tokens = sent_tokenize(input_str)  </span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(tokens), tokens)  </span><br><span class="line">  </span><br><span class="line">words = []  </span><br><span class="line"><span class="keyword">for</span> token <span class="keyword">in</span> tokens:  </span><br><span class="line">    words.append([word.lower() <span class="keyword">for</span> word <span class="keyword">in</span> word_tokenize(token)])  </span><br><span class="line"><span class="built_in">print</span>(words)</span><br><span class="line">tags = []  </span><br><span class="line"><span class="keyword">for</span> word <span class="keyword">in</span> words:  </span><br><span class="line">    tags.append(pos_tag(word))  </span><br><span class="line"><span class="built_in">print</span>(tags)</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">&lt;class &#x27;list&#x27;&gt; [&#x27;A wrapper around a sequence of simple (string) tokens.&#x27;, &#x27;which is intended to support initial exploration of texts (via the interactive console).&#x27;]</span></span><br><span class="line"><span class="string">[[&#x27;a&#x27;, &#x27;wrapper&#x27;, &#x27;around&#x27;, &#x27;a&#x27;, &#x27;sequence&#x27;, &#x27;of&#x27;, &#x27;simple&#x27;, &#x27;(&#x27;, &#x27;string&#x27;, &#x27;)&#x27;, &#x27;tokens&#x27;, &#x27;.&#x27;], [&#x27;which&#x27;, &#x27;is&#x27;, &#x27;intended&#x27;, &#x27;to&#x27;, &#x27;support&#x27;, &#x27;initial&#x27;, &#x27;exploration&#x27;, &#x27;of&#x27;, &#x27;texts&#x27;, &#x27;(&#x27;, &#x27;via&#x27;, &#x27;the&#x27;, &#x27;interactive&#x27;, &#x27;console&#x27;, &#x27;)&#x27;, &#x27;.&#x27;]]</span></span><br><span class="line"><span class="string">[[(&#x27;a&#x27;, &#x27;DT&#x27;), (&#x27;wrapper&#x27;, &#x27;NN&#x27;), (&#x27;around&#x27;, &#x27;IN&#x27;), (&#x27;a&#x27;, &#x27;DT&#x27;), (&#x27;sequence&#x27;, &#x27;NN&#x27;), (&#x27;of&#x27;, &#x27;IN&#x27;), (&#x27;simple&#x27;, &#x27;JJ&#x27;), (&#x27;(&#x27;, &#x27;(&#x27;), (&#x27;string&#x27;, &#x27;VBG&#x27;), (&#x27;)&#x27;, &#x27;)&#x27;), (&#x27;tokens&#x27;, &#x27;NNS&#x27;), (&#x27;.&#x27;, &#x27;.&#x27;)], [(&#x27;which&#x27;, &#x27;WDT&#x27;), (&#x27;is&#x27;, &#x27;VBZ&#x27;), (&#x27;intended&#x27;, &#x27;VBN&#x27;), (&#x27;to&#x27;, &#x27;TO&#x27;), (&#x27;support&#x27;, &#x27;VB&#x27;), (&#x27;initial&#x27;, &#x27;JJ&#x27;), (&#x27;exploration&#x27;, &#x27;NN&#x27;), (&#x27;of&#x27;, &#x27;IN&#x27;), (&#x27;texts&#x27;, &#x27;NN&#x27;), (&#x27;(&#x27;, &#x27;(&#x27;), (&#x27;via&#x27;, &#x27;IN&#x27;), (&#x27;the&#x27;, &#x27;DT&#x27;), (&#x27;interactive&#x27;, &#x27;JJ&#x27;), (&#x27;console&#x27;, &#x27;NN&#x27;), (&#x27;)&#x27;, &#x27;)&#x27;), (&#x27;.&#x27;, &#x27;.&#x27;)]]</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>


<h3 id="4-Text-对象属性，方法"><a href="#4-Text-对象属性，方法" class="headerlink" title="4. Text 对象属性，方法"></a>4. Text 对象属性，方法</h3><h4 id="4-1-count"><a href="#4-1-count" class="headerlink" title="4.1 count()"></a>4.1 <code>count()</code></h4><ul>
<li>统计对象中某个词出现的次数<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> nltk.text <span class="keyword">import</span> Text</span><br><span class="line">t = Text(words[<span class="number">0</span>])  </span><br><span class="line">t.count(<span class="string">&quot;a&quot;</span>)</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="4-2-index"><a href="#4-2-index" class="headerlink" title="4.2 index()"></a>4.2 <code>index()</code></h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> nltk.text <span class="keyword">import</span> Text</span><br><span class="line">t = Text(words[<span class="number">0</span>])  </span><br><span class="line">t.index(<span class="string">&quot;a&quot;</span>)</span><br></pre></td></tr></table></figure>

<h4 id="4-3-plot-以图表形式显示前几个最多的次数"><a href="#4-3-plot-以图表形式显示前几个最多的次数" class="headerlink" title="4.3 plot() 以图表形式显示前几个最多的次数"></a>4.3 <code>plot()</code> 以图表形式显示前几个最多的次数</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> nltk.text <span class="keyword">import</span> Text</span><br><span class="line">t = Text(words[<span class="number">0</span>])</span><br><span class="line">t.plot(<span class="number">3</span>)  </span><br></pre></td></tr></table></figure>

<h3 id="5-停用词过滤"><a href="#5-停用词过滤" class="headerlink" title="5. 停用词过滤"></a>5. 停用词过滤</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="keyword">from</span> nltk.corpus <span class="keyword">import</span> stopwords  </span><br><span class="line">  </span><br><span class="line"><span class="comment"># 停用词说明介绍  </span></span><br><span class="line"><span class="built_in">print</span>(stopwords.readme().replace(<span class="string">&quot;\n&quot;</span>, <span class="string">&quot; &quot;</span>))  </span><br><span class="line"><span class="comment"># 展示停用词表的个数  </span></span><br><span class="line"><span class="built_in">print</span>(stopwords.fileids())  </span><br><span class="line"><span class="comment"># 查看某个停用词表中的词  </span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(stopwords.raw(<span class="string">&quot;finnish&quot;</span>)), stopwords.raw(<span class="string">&quot;finnish&quot;</span>).replace(<span class="string">&quot;\n&quot;</span>, <span class="string">&quot;/&quot;</span>))</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">Stopwords Corpus  This corpus contains lists of stop words for several languages.  These are high-frequency grammatical words which are usually ignored in text retrieval applications.  They were obtained from: http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/backend/snowball/stopwords/  The stop words for the Romanian language were obtained from: http://arlc.ro/resources/  The English list has been augmented https://github.com/nltk/nltk_data/issues/22  The German list has been corrected https://github.com/nltk/nltk_data/pull/49  A Kazakh list has been added https://github.com/nltk/nltk_data/pull/52  A Nepali list has been added https://github.com/nltk/nltk_data/pull/83  An Azerbaijani list has been added https://github.com/nltk/nltk_data/pull/100  A Greek list has been added https://github.com/nltk/nltk_data/pull/103  An Indonesian list has been added https://github.com/nltk/nltk_data/pull/112 </span></span><br><span class="line"><span class="string">[&#x27;arabic&#x27;, &#x27;azerbaijani&#x27;, &#x27;danish&#x27;, &#x27;dutch&#x27;, &#x27;english&#x27;, &#x27;finnish&#x27;, &#x27;french&#x27;, &#x27;german&#x27;, &#x27;greek&#x27;, &#x27;hungarian&#x27;, &#x27;indonesian&#x27;, &#x27;italian&#x27;, &#x27;kazakh&#x27;, &#x27;nepali&#x27;, &#x27;norwegian&#x27;, &#x27;portuguese&#x27;, &#x27;romanian&#x27;, &#x27;russian&#x27;, &#x27;slovene&#x27;, &#x27;spanish&#x27;, &#x27;swedish&#x27;, &#x27;tajik&#x27;, &#x27;turkish&#x27;]</span></span><br><span class="line"><span class="string">&lt;class &#x27;str&#x27;&gt; olla/olen/olet/on/olemme/olette/ovat/ole/oli/olisi/olisit/olisin/olisimme/olisitte/olisivat/olit/olin/olimme/olitte/olivat/ollut/olleet/en/et/ei/emme/ette/eivät/minä/minun/minut/minua/minussa/minusta/minuun/minulla/minulta/minulle/sinä/sinun/sinut/sinua/sinussa/sinusta/sinuun/sinulla/sinulta/sinulle/hän/hänen/hänet/häntä/hänessä/hänestä/häneen/hänellä/häneltä/hänelle/me/meidän/meidät/meitä/meissä/meistä/meihin/meillä/meiltä/meille/te/teidän/teidät/teitä/teissä/teistä/teihin/teillä/teiltä/teille/he/heidän/heidät/heitä/heissä/heistä/heihin/heillä/heiltä/heille/tämä/tämän/tätä/tässä/tästä/tähän/tallä/tältä/tälle/tänä/täksi/tuo/tuon/tuotä/tuossa/tuosta/tuohon/tuolla/tuolta/tuolle/tuona/tuoksi/se/sen/sitä/siinä/siitä/siihen/sillä/siltä/sille/sinä/siksi/nämä/näiden/näitä/näissä/näistä/näihin/näillä/näiltä/näille/näinä/näiksi/nuo/noiden/noita/noissa/noista/noihin/noilla/noilta/noille/noina/noiksi/ne/niiden/niitä/niissä/niistä/niihin/niillä/niiltä/niille/niinä/niiksi/kuka/kenen/kenet/ketä/kenessä/kenestä/keneen/kenellä/keneltä/kenelle/kenenä/keneksi/ketkä/keiden/ketkä/keitä/keissä/keistä/keihin/keillä/keiltä/keille/keinä/keiksi/mikä/minkä/minkä/mitä/missä/mistä/mihin/millä/miltä/mille/minä/miksi/mitkä/joka/jonka/jota/jossa/josta/johon/jolla/jolta/jolle/jona/joksi/jotka/joiden/joita/joissa/joista/joihin/joilla/joilta/joille/joina/joiksi/että/ja/jos/koska/kuin/mutta/niin/sekä/sillä/tai/vaan/vai/vaikka/kanssa/mukaan/noin/poikki/yli/kun/niin/nyt/itse/</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>XiaoPang
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://zhangyh.fun/2021/08/13/NLTK%20%E6%A8%A1%E5%9D%97%E8%AF%A6%E8%A7%A3/" title="NLTK 模块详解">https://zhangyh.fun/2021/08/13/NLTK 模块详解/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://zhangyh.fun/2021/08/13/NLTK%20%E6%A8%A1%E5%9D%97%E8%AF%A6%E8%A7%A3/"  aria-label="复制成功！"></i></span>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>
<script>
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({
          title: "",
          text: '复制成功',
          icon: "success",
          showConfirmButton: true
          });
        });
    });
</script>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/python/" rel="tag"><i class="fa fa-tag"></i> python</a>
              <a href="/tags/NLTK/" rel="tag"><i class="fa fa-tag"></i> NLTK</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/13/NPM%20%E5%8C%85%E7%AE%A1%E7%90%86%E5%99%A8%E8%AF%A6%E8%A7%A3/" rel="prev" title="NPM 包管理器详解">
      <i class="fa fa-chevron-left"></i> NPM 包管理器详解
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/13/Docker/" rel="next" title="Docker 教程">
      Docker 教程 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    

  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#NLTK-%E6%A8%A1%E5%9D%97%E8%AF%A6%E8%A7%A3"><span class="nav-number">1.</span> <span class="nav-text">NLTK 模块详解</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.1.</span> <span class="nav-text">1. 介绍</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%AE%89%E8%A3%85%E6%A8%A1%E5%9D%97"><span class="nav-number">1.2.</span> <span class="nav-text">2. 安装模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%9F%BA%E7%A1%80%E6%96%B9%E6%B3%95"><span class="nav-number">1.3.</span> <span class="nav-text">3. 基础方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-%E6%96%87%E6%9C%AC%E6%8C%89%E7%85%A7%E5%8F%A5%E5%AD%90%E8%BF%9B%E8%A1%8C%E5%88%86%E5%89%B2-sent-tokenize"><span class="nav-number">1.3.1.</span> <span class="nav-text">3.1 文本按照句子进行分割 sent_tokenize()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-%E5%AF%B9%E5%8F%A5%E5%AD%90%E8%BF%9B%E8%A1%8C%E5%88%86%E8%AF%8D-word-tokenize"><span class="nav-number">1.3.2.</span> <span class="nav-text">3.2 对句子进行分词 word_tokenize()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-%E5%88%86%E8%AF%8D%E8%AF%8D%E6%80%A7%E6%A0%87%E6%B3%A8-pos-tag"><span class="nav-number">1.3.3.</span> <span class="nav-text">3.3 分词词性标注 pos_tag()</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-Text-%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%EF%BC%8C%E6%96%B9%E6%B3%95"><span class="nav-number">1.4.</span> <span class="nav-text">4. Text 对象属性，方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-1-count"><span class="nav-number">1.4.1.</span> <span class="nav-text">4.1 count()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-index"><span class="nav-number">1.4.2.</span> <span class="nav-text">4.2 index()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-3-plot-%E4%BB%A5%E5%9B%BE%E8%A1%A8%E5%BD%A2%E5%BC%8F%E6%98%BE%E7%A4%BA%E5%89%8D%E5%87%A0%E4%B8%AA%E6%9C%80%E5%A4%9A%E7%9A%84%E6%AC%A1%E6%95%B0"><span class="nav-number">1.4.3.</span> <span class="nav-text">4.3 plot() 以图表形式显示前几个最多的次数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E5%81%9C%E7%94%A8%E8%AF%8D%E8%BF%87%E6%BB%A4"><span class="nav-number">1.5.</span> <span class="nav-text">5. 停用词过滤</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="XiaoPang"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">XiaoPang</p>
  <div class="site-description" itemprop="description">少说，多做</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">34</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhang123-123" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhang123-123" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/1741290057@qq.com" title="E-Mail → 1741290057@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


<!-- CloudCalendar -->
<div class="widget-wrap" style="width: 90%;margin-left: auto;margin-right: auto; opacity: 0.97;">
	<div class="widget" id="CloudCalendar"></div>
</div>
      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XiaoPang</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
    <script type="text/javascript" color="0,0,0" opacity='0.5' zIndex="-2" count="200" src="/js/my_js/canvas-nest.min.js"></script>
    

</body>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/my_js/clicklove.js"></script>

<!-- calendar widget -->

    <script src="/lib/calendar/calendar.min.js"></script>
    <script src="/lib/calendar/languages.min.js"></script>
    <script type="text/javascript">
    $(function() {
        $('#CloudCalendar').aCalendar('zh-CN',
            $.extend(
                '', {
                    single:true,
                    root:'/calendar/'
                }
            )
        );
    });
    </script>


</html>
